package org.lq.er.view.subject;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import org.lq.er.entity.Subject;
import org.lq.er.service.SubjectService;
import org.lq.er.service.impl.SubjectServiceImpl;
import org.lq.er.util.CastUtil;
import org.lq.er.util.DateUtil;

import java.awt.Color;

public class V_subject_main extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable table;
	private SubjectService ss = new SubjectServiceImpl();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_subject_main frame = new V_subject_main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_subject_main() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 803, 411);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setBounds(10, 271, 767, 91);
		contentPane.add(panel);
		panel.setLayout(null);

		JButton btn_Query = new JButton("查询");
		btn_Query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				initTable();
			}
		});
		btn_Query.setBounds(54, 10, 93, 23);
		panel.add(btn_Query);

		JButton btn_Quit = new JButton("退出");
		btn_Quit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btn_Quit.setBounds(621, 10, 93, 23);
		panel.add(btn_Quit);

		JButton btn_Delete = new JButton("删除");
		btn_Delete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if (index >= 0) {
					int n = JOptionPane.showConfirmDialog(V_subject_main.this,
							"确定删除" + table.getValueAt(index, 1) + "科目吗?", "删除", JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE);
					if (n == 0) {
						int id = CastUtil.castInt(table.getValueAt(index, 0));
						if (new SubjectServiceImpl().delete(id)) {
							JOptionPane.showMessageDialog(V_subject_main.this, "删除成功!");
							initTable();
						} else {
							JOptionPane.showMessageDialog(V_subject_main.this, "删除失败!");
						}
					}
				} else {
					JOptionPane.showMessageDialog(V_subject_main.this, "请选择需要删除的科目");
				}
				initTable();
			}
		});
		btn_Delete.setBounds(197, 10, 93, 23);
		panel.add(btn_Delete);

		JButton btn_Add = new JButton("添加");
		btn_Add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_subject_add add = new V_subject_add();
				add.setVisible(true);
				add.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						initTable();
					}
				});
			}
		});
		btn_Add.setBounds(341, 10, 93, 23);
		panel.add(btn_Add);

		JButton btn_Add_1 = new JButton("修改");
		btn_Add_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int index = table.getSelectedRow();
				if (index >= 0) {
					V_subject_update update = new V_subject_update(CastUtil.castInt(table.getValueAt(index, 0)));
					update.setVisible(true);
					update.addWindowListener(new WindowAdapter() {
						@Override
						public void windowClosed(WindowEvent e) {
							//当添加窗体关闭的时候,调用表格初始化方法
							initTable();
						}
						@Override
						public void windowClosing(WindowEvent e) {
						}
					});
				} else {
					JOptionPane.showMessageDialog(V_subject_main.this, "请选择需要修改的科目");
				}
			}
		});
		btn_Add_1.setBounds(481, 10, 93, 23);
		panel.add(btn_Add_1);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 20, 767, 241);
		contentPane.add(scrollPane);

		table = new JTable();
		table.setBackground(Color.LIGHT_GRAY);
		table.setForeground(Color.WHITE);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "\u79D1\u76EE", "\u62A5\u540D\u5F00\u59CB\u65F6\u95F4", "\u62A5\u540D\u622A\u6B62\u65F6\u95F4", "\u8003\u8BD5\u5F00\u59CB\u65F6\u95F4", "\u8003\u8BD5\u65F6\u957F", "\u8D39\u7528"
			}
		));
		table.getColumnModel().getColumn(2).setPreferredWidth(89);
		table.getColumnModel().getColumn(3).setPreferredWidth(89);
		table.getColumnModel().getColumn(4).setPreferredWidth(92);
		scrollPane.setViewportView(table);
		initTable();
	}

	// 展示信息
	public void initTable() {
		initTable(ss.findAll());
	}
	public void initTable(List<Subject> list) {
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		int count = dtm.getRowCount();
		for (int i = 0; i < count; i++) {
			dtm.removeRow(0);
		}

		for (Subject subject : list) {
			dtm.addRow(new Object[] { 
					subject.getId(),
					subject.getSubjectName(),
					subject.getStartDate(),
					subject.getEndDate(),
					DateUtil.toStr(subject.getBeginTime()),
//					subject.getDetailTime(),
					subject.getDuration(),
					subject.getCost() 
					});
		}
	}

}
